package com.example.shopping_mall.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shopping_mall.R;
import com.example.shopping_mall.bean.UiBrandBean;
import com.example.shopping_mall.bean.UiGoodFreshBean;

import java.util.List;

//品牌制造商列表
public class BrandAdapter extends RecyclerView.Adapter<BrandAdapter.BrandHolder>{

    private Context context;
    private List<UiBrandBean> brandBeans;
    private onBrandClickListener brandClickListener;

    public BrandAdapter(Context context) {
        this.context = context;
    }

    public void setBrandBeans(List<UiBrandBean> brandBeans) {
        this.brandBeans = brandBeans;
    }

    public void setBrandClickListener(onBrandClickListener brandClickListener) {
        this.brandClickListener = brandClickListener;
    }

    @NonNull
    @Override
    public BrandHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BrandHolder(LayoutInflater.from(context).inflate(R.layout.brand_manufacturer_list_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull BrandHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.brandImage.setImageBitmap(brandBeans.get(position).getBrandImage());
        holder.maker.setText(brandBeans.get(position).getMaker());

        if (brandClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    brandClickListener.onBrandClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null==brandBeans?0:brandBeans.size();
    }

    class BrandHolder extends RecyclerView.ViewHolder{

        ImageView brandImage;
        TextView maker;

        public BrandHolder(@NonNull View itemView) {
            super(itemView);

            brandImage=itemView.findViewById(R.id.brand_image);
            maker=itemView.findViewById(R.id.maker);
        }
    }

    public interface onBrandClickListener{
        void onBrandClick(int position);
    }
}
